package com.mogist.hqc.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.mogist.hqc.R;

import java.lang.reflect.Method;

import talex.zsw.basecore.util.DimenTool;
import talex.zsw.basecore.util.RegTool;

/**
 * 作用：通用方法
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
public class Util
{
	public static String getTimeStr(String time)
	{
		if(RegTool.isNotEmpty(time))
		{
			if(time.length() > 9)
			{
				return time.substring(0, 10);
			}
			else
			{
				return time;
			}
		}
		else
		{
			return "";
		}
	}

	public static String getOrderStatus(String status, String pay)
	{
		String s = status;
		// NEW("NEW", "新建订单"), FAILED("FAILED", "失效订单"), SUCCESS("SUCCESS", "成功订单"), FINISH("FINISH", "结束订单"), APPRAISED("APPRAISED", "已评价");REFUNDING("REFUNDING", "退款中");
		if("NEW".equals(status))
		{
			s = "待付款";
		}
		else if("FAILED".equals(status))
		{
			s = "已失效";
		}
		else if("SUCCESS".equals(status))
		{
			s = "待出行";
		}
		else if("FINISH".equals(status))
		{
			if("PAYED".equals(pay))
			{
				s = "待评价";
			}
			else
			{
				s = "已取消";
			}
		}
		else if("APPRAISED".equals(status))
		{
			s = "已完成";
		}
		else if("REFUNDING".equals(status))
		{
			s = "退款中";
		}
		return s;
	}

	public static String getPayStatus(String status)
	{
		String s = status;
		// UNPAY("UNPAY", "未支付"), PAYED("PAYED", "已支付");
		if("UNPAY".equals(status))
		{
			s = "未支付";
		}
		else if("PAYED".equals(status))
		{
			s = "已支付";
		}
		return s;
	}

	public static String getPayType(String status)
	{
		String s = status;
		// WECHAT("WECHAT", "微信支付"), ALIPAY("ALIPAY", "支付宝支付");
		if("WECHAT".equals(status))
		{
			s = "微信支付";
		}
		else if("ALIPAY".equals(status))
		{
			s = "支付宝支付";
		}
		return s;
	}

	public static Bitmap createScreenShotBitmap(Context context, Bitmap bitmap)
	{
		View v = LayoutInflater.from(context).inflate(R.layout.share_screenshot_layout, null);
		ImageView iv = (ImageView) v.findViewById(R.id.iv);
		iv.setImageBitmap(bitmap);

		//整体布局
		Point point = getRealScreenSize(context);
		v.measure(View.MeasureSpec.makeMeasureSpec(point.x, View.MeasureSpec.EXACTLY), View.MeasureSpec
			.makeMeasureSpec(point.y, View.MeasureSpec.EXACTLY));

		v.layout(0, 0, point.x, point.y);

		//        Bitmap result = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);
		Bitmap result = Bitmap.createBitmap(v.getWidth(), v.getHeight()+
			DimenTool.dp2px(140), Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(result);
		c.drawColor(Color.WHITE);
		// Draw view to canvas
		v.draw(c);
		bitmap.recycle();
		return result;
	}

	private static Point getRealScreenSize(Context context)
	{
		Point screenSize = null;
		try
		{
			screenSize = new Point();
			WindowManager windowManager
				= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display defaultDisplay = windowManager.getDefaultDisplay();
			if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
			{
				defaultDisplay.getRealSize(screenSize);
			}
			else
			{
				try
				{
					Method mGetRawW = Display.class.getMethod("getRawWidth");
					Method mGetRawH = Display.class.getMethod("getRawHeight");
					screenSize.set((Integer) mGetRawW.invoke(defaultDisplay), (Integer) mGetRawH.invoke(defaultDisplay));
				}
				catch(Exception e)
				{
					screenSize.set(defaultDisplay.getWidth(), defaultDisplay.getHeight());
					e.printStackTrace();
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return screenSize;
	}

	public static Bitmap mosaicBitmapVertical(@NonNull Bitmap... bitmaps)
	{
		int height = 0;
		for(int i = 0; i < bitmaps.length; i++)
		{
			height += bitmaps[i].getHeight();
		}
		int width = 0;
		for(int i = 0; i < bitmaps.length; i++)
		{
			if(width < bitmaps[i].getWidth())
			{
				width = bitmaps[i].getWidth();
			}
		}
		Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		int drawHeight = 0;
		for(int i = 0; i < bitmaps.length; i++)
		{
			canvas.drawBitmap(bitmaps[i], 0, drawHeight, null);
			drawHeight += bitmaps[i].getHeight();
		}
		return result;
	}
}